#include <iostream>
#include <fstream>
#include <ios>
#include <vector>
#include "cCaenEvent.hh"
#include "TFile.h"
#include "cInputInfo.hh"
#include "EnergyCalibration.hh"
#include "TH1F.h"
#include "TH2F.h"

//class cInputInfo;
using namespace std;
//class EnergyCalibration;
//! Class that stores a list of events and has the ability to analise the list
class cEventList {
public:
	cEventList();
	cEventList(char name[256]); //!<create a list with name
	cEventList(int id); //!< create a list with name starting from id
	char name[256]; //!< the name of the list

	double markedPileUp; //!<how many events in the list were marked as pile-up
	double markedDeadTime; //!<how many events in the list were "rejected" because of deadtime
	double realTime; //!< the real time of the run; computed as time difference between the last and first event
	double startTime; //!< the timestamp of the first event
	double lifeTime; //!< the life time of the run; computed as a sum of differences between the timestamp of an accepted event and the last safetime
	double deadTime; //!< the deadtime computed from list information
	void DoOtherMath(); //!< computes auxiliary numbers for the list (real time...)
	double maxTforHisto; //!< the maximum time value to be used in time histograms;
	int noBinsForHisto; //!< the number of bins to be used in time histograms;

	vector<cCaenEvent> data; //!< the event data itself
	//! returns the total number of events in the list (including those marked as dead)
	int size() {
		return data.size();
	}
	;
	friend ostream& operator<<(ostream& output, const cEventList& p);
	friend istream& operator>>(istream& input, cEventList& p);

//		int ReadFile(stringstream *ss); //!< read a list from a file
	int ReadFileBin(stringstream *ss); //!<read a list from a binary file
	int ReadFileBin(stringstream *ss, int detId); //!<read a list from a binary file saving only the events from detId
//		int ReadFromDir(char dirname[256]); //!<read all the lists from a directory
//						int getFromListId(cEventList*,int); //!<create a new list by selecting a certain detector number from the full list
	int ReadFromDir(char dirname[256], int cardNo); //!<read all the lists from a directory for cardNo
	//int ReadFromDir(char dirname[256], int detId, int cardNo); //!<read all the lists from a directory for cardNo saving only the events from detId
	int ReadFromDir(char dirname[256], int cardNo, int fileNo); //!<read a single  list from a directory for cardNo
	void Print(); //!< ouotputs info about the list to cout
//
//		void BuildPileUpDistr(); //!<builds the ET distribution of events marked as pile-up by the acquisition (TH2F named pileupdistET)
//		void BuildETMatrix(); //!<builds the ET distribution of events: histET for those not rejected by DT and histET_rejectedDT for those rejected
//		void markDeadTime(int type , int value); //!< marks the events in the list as isDead=1 or isDead=0 according to the deadtime type and deadtime value
//			void markDeadTimeNEDT(double); //!<does the NEDT marking
//			void markDeadTimeEDT(double);  //!<EDT marking
//		void BuildTimeIntervalDistribution(int elow, int ehigh, int scale); //!< build the time interval distribution for events in the energy range; the max time value of the time scale in the histogram is 100*scale/*10E-6; (if scale is the deadtime in microseconds, the maxtime is 100 times the deadtime, expressed in seconds)
//		void BuildTimeIntervalDistribution(int elow, int ehigh, int nBins, float max); //!< build the time interval distribution for events in the energy range; the max time value of the time scale and the number of bins are passed as parameters (used in runmode 3)
//		void BuildTimeAnalysisMatrixSimple(int e1low, int e1high,int e2low, int e2high, double deltaT); //!< do the Time interval analysis; the time is reset each time an event in the first energy gate is found
//		void BuildTimeAnalysisMatrixSimple2(int e1low, int e1high,int e2low, int e2high, double deltaT); //!< do the Time interval analysis; the time is NOT reset each time an event in the first energy gate is found
//		void BuildTimeAnalysisMatrixSimple3(int e1low, int e1high,int e2low, int e2high, double deltaT); //!< do the Time interval analysis; uses tove techinique as described by radeloff (nim 47 (1967),109)
//
//		void WriteFileData(TFile *f, cInputInfo *info); //! writes the stats histograms on the root file

	vector<int> detIds; //list of detectorNos that is read from the file
	vector<TH1F*> detSpectra; //list of spectra for each detNo from the file
	TH1F* addBackSpec;
	vector<TH1F*> detTID; //list of TID for each detNo from the file
	vector<TH2F*>matrices;//list of E-T matrices for each detNo in the file
	TH2F* addBackMat;
	vector<double> lastTimeStamp; //last timestamp for each detector
	TH1F* globalTID; //TID for all detectors
	TH1F* globalTID2; //TID for all detectors skipping one event
	TH1F* globalTID3; //TID for all detectors skipping 2 events
	TH1F* globalTID4; //TID for all detectors skipping 3 events
	double currentTimeStamp; //the current time
	vector <EnergyCalibration>enCal;


	void MakeDetectorList();
	void CalibrateEnergy();
	void MakeEmptySpectras();
	void MakeSpectraSingle();
	void WriteSpectraSingle(char dirname[256],int cardNo);
	void WriteResults(char dirName[256], int cardNo);
	void ReadCalibration(char dirname[256], int cardNo);
	void MakeSpectraWithPileUp();
	void MakeTIDSingle();
	void MakeTIDAll();
	void MakeMatrices();

	int eventsStored;
	void CleanData();
};
